package org.shoukaiseki.tools.mybatis.generator.plugin.oracle;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.shoukaiseki.commons.utils.NameUtils;
import org.shoukaiseki.commons.utils.oracle.OracleCommentUtils;
import org.shoukaiseki.commons.utils.oracle.OracleDataTypeUtils;
import org.shoukaiseki.tools.mybatis.generator.actuator.BaseGeneraterJavaModel;
import org.shoukaiseki.tools.mybatis.generator.config.SqlProperties;
import org.shoukaiseki.tools.mybatis.generator.velocity.domain.ColumnInfo;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description TODO: 数据读取
 * @author Rick
 * @Date 2018/9/21 9:52
 * @Vesion 1.0.0
 */
@Slf4j
public class OracleToGeneraterJavaModel extends BaseGeneraterJavaModel {

    OracleCommentUtils commentUtils;
    Connection connection;

    public OracleToGeneraterJavaModel(Connection connection) throws SQLException {
       this.connection=connection;
      commentUtils=new OracleCommentUtils(connection);
    }

    private Map<String,String> tableInfo = Maps.newHashMap();

    @Override
    public Map<String, String> getTableNameMap() {
        return tableInfo;
    }

    @Override
    public List<ColumnInfo> generator(ResultSetMetaData metaData, SqlProperties sqlProperties, String savePath) throws SQLException {
        String modelName=sqlProperties.getDomainObjectName();
        String packageName=sqlProperties.getJavaModelPackage();


        List<ColumnInfo> columnInfoList= Lists.newArrayList();

        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            String schemaName = metaData.getSchemaName(i);
            String tableName = metaData.getTableName(i);
            tableName =sqlProperties.getTableName();
            String columnName = metaData.getColumnName(i);
            log.error("columnName={},columnType={}",columnName,metaData.getColumnType(i));
            String[] dataTypes = OracleDataTypeUtils.getJdbcTypeFromColumnType(metaData.getColumnType(i));

            ColumnInfo columnInfo=new ColumnInfo();
            columnInfo.setTableName("");
            if(StringUtils.isEmpty(tableName)){
                columnInfo.setTableName("");
            }else{
                Optional<String> tableComment = commentUtils.getTableComment(schemaName,tableName);
                if(sqlProperties.isShowTableName()){
                    if(tableComment.isPresent()){
                        if(sqlProperties.isShowTableComment()){
                            columnInfo.setTableName(tableComment.get()+"["+tableName+"]");
                        }else{
                            columnInfo.setTableName(tableName);
                        }
                    }else{
                        columnInfo.setTableName(tableName);
                    }
                }
                if(!tableInfo.containsKey(tableName)){
                    if(tableComment.isPresent()){
                        tableInfo.put(tableName,tableComment.get());
                    }else{
                        tableInfo.put(tableName,tableName);
                    }
                }
            }

            columnInfo.setColumnName(columnName);

            Optional<String> columnComment = commentUtils.getColumnComment(schemaName,tableName, columnName);
            boolean primaryKey =false;
            if(columnName.equalsIgnoreCase(sqlProperties.getPrimaryKey())){
                primaryKey=true;
            }
            columnInfo.setPrimaryKey(primaryKey);
            if(columnComment.isPresent()){
                    columnInfo.setColumnComment(columnComment.get());
            }else{
                columnInfo.setColumnComment("");
            }

            String attrname = NameUtils.humpName(metaData.getColumnLabel(i));
            columnInfo.setAttrname(attrname);
            columnInfo.setAttrName(NameUtils.firstLetterUppercase(attrname));
            //java属性类型
            columnInfo.setAttrType(dataTypes[0]);
            //数据库属性类型
            columnInfo.setJdbcType(dataTypes[2]);

            columnInfo.setColumnLabel(metaData.getColumnLabel(i));

            columnInfoList.add(columnInfo);
        }


        return columnInfoList;
    }
}
